﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.data {
    import flash.events.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.fountain.*;
    import net.wooga.pets.models.world.*;

    public class UpdateUserDataCommand extends Command {

        public var event:InfoEvent;
        public var userModel:UserModel;
        public var friendsModel:FriendsModel;
        public var roomSlotsModel:RoomSlotsModel;
        public var gameStateModel:GameStateModel;
        public var energyModel:EnergyModel;
        public var symbolsModel:SymbolModel;
        public var inventoryModel:InventoryModel;
        public var missionModel:MissionModel;
        public var worldModel:WorldModel;
        public var collectableModel:CollectableModel;

        override public function execute():void{
            var _local1:Object = this.event.info;
            if (_local1.elder){
                dispatch(new InfoEvent(EventConstants.LOAD_PATIENT_ASSET, null, _local1.elder));
            };
            this.updateUserData(((_local1.changes) || (_local1)));
            if (_local1.symbols){
                this.symbolsModel.symbols = _local1.symbols;
            };
            if (_local1.inventory){
                this.inventoryModel.inventory = _local1.inventory;
            };
            if (_local1.collectable){
                this.collectableModel.updateCollectableByType(_local1.collectable.pet_type, _local1.collectable.index, _local1.collectable.reward_type);
            };
            if (this.friendsModel.updateFriendsInCount <= 0){
                this.friendsModel.updateFriendsInCount = GameConstants.UPDATE_FRIENDSLIST_INTERVAL;
                this.updateFriendsList();
            } else {
                this.friendsModel.updateFriendsInCount--;
            };
            dispatch(new ShopEvent(ShopEvent.UPDATE_SHOP_ITEMS));
        }
        private function updateFriendsList():void{
            var _local1:int;
            var _local4:Object;
            var _local2:Array = this.friendsModel.friends;
            var _local3:Object = {};
            for each (_local4 in _local2) {
                if (_local4.uid == this.userModel.mySocialID){
                    _local1 = _local2.indexOf(_local4);
                    _local3 = _local2.splice(_local1, 1)[0];
                    _local3.xp = this.userModel.xpPoints;
                    _local3.level = this.userModel.level;
                    this.friendsModel.friends.push(_local3);
                    this.friendsModel.friends.sort();
                    this.friendsModel.sortFriends();
                    dispatch(new Event(CommandConstants.ADD_FRIENDS));
                };
            };
        }
        private function updateUserData(_arg1:Object):void{
            var _local2:Array;
            var _local3:Array;
            if (!_arg1){
                return;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_COINS_NEW)){
                this.userModel.coins = _arg1.coins_new;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_XP_NEW)){
                this.userModel.xpPoints = _arg1.xp_new;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_BATTERIES)){
                this.userModel.batteries = _arg1.batteries;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_COFEE)){
                this.userModel.coffee = _arg1.coffee;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_GOODS)){
                this.userModel.goods = _arg1.goods_new;
            };
            if (_arg1.level_info){
                if (_arg1.level_info.room_slots){
                    this.roomSlotsModel.availableRoomSlotsNumber = _arg1.level_info.room_slots;
                };
                if (_arg1.level_info.xp_bounds){
                    this.userModel.xpBounds = _arg1.level_info.xp_bounds;
                };
                this.userModel.setLevel(_arg1);
                this.userModel.levelUpCoins = _arg1.level_info.level_up_coins;
                this.userModel.numberOfCuredPets = ((_arg1.level_info.number_of_cured_pets) || (0));
                this.userModel.numberOfBirdsCollected = ((_arg1.level_info.number_of_birds_collected) || (0));
                this.userModel.numberOfUsedEnergy = ((_arg1.level_info.amount_of_used_energy) || (0));
                this.userModel.numberOfFedDoctors = ((_arg1.level_info.number_of_fed_doctors) || (0));
                this.userModel.numberOfCuredElders = ((_arg1.level_info.number_of_cured_elders) || (0));
                this.userModel.numberOfCollectedSymbols = ((_arg1.level_info.number_of_collected_symbols) || (0));
            };
            if (_arg1.hasOwnProperty("queue_added")){
                _local2 = _arg1.queue_added;
                dispatch(new InfoEvent(EventConstants.ADD_NEW_PET_TO_QUEUE, null, _local2));
            };
            if (_arg1.hasOwnProperty(GameConstants.ENERGY)){
                this.energyModel.energyStartedAt = _arg1.energy.started_at;
                this.energyModel.energyUsed = _arg1.energy.used;
                this.energyModel.energyMax = _arg1.energy.max_value;
                this.energyModel.energyRefillDuration = _arg1.energy.refill_duration;
                this.energyModel.energyExtra = _arg1.energy.extra;
            };
            if (_arg1.hasOwnProperty(GameConstants.USER_DATA_LEVEL_MISSIONS_NEW)){
                _local3 = _arg1.level_missions_new;
                if (((_local3) && ((_local3.length > 0)))){
                    this.missionModel.addLevelMissions(_arg1.level_missions_new, true);
                };
            };
            dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
        }

    }
}//package net.wooga.pets.controllers.data 
